package com.zhisheng.gufei.web.action.user;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;

import com.demo2do.lighturl.annotation.Action;
import com.zhisheng.core.security.entity.Role;
import com.zhisheng.core.security.entity.User;
import com.zhisheng.core.utils.MD5;
import com.zhisheng.core.web.action.BaseAction;
import com.zhisheng.gufei.city.service.CityService;
import com.zhisheng.gufei.user.service.RoleService;
import com.zhisheng.gufei.user.service.UserService;

public class UserAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6546951490456862370L;
	
	@Resource
	private UserService userService;
	
	@Resource
	private RoleService roleService;
	@Resource
	private CityService cityService;
	
	private User user;
	private List<Role> roles;
	private List<Integer> roleIds;
	private String oldpassword;
	
	private String citydatas="";
	
	@Action("/user/list")
	public String list() throws Exception{
	    userService.findByPage(createPage(), User.class);	
		return "list";
	}

	@Action("/user/add")
	public String add() throws Exception{
		JSONArray jsonObject = JSONArray.fromObject(cityService.getCityNodeList(cityService.getOriginCity()));
		 citydatas = jsonObject.toString();
		 return "add";
	}

	
	@Action("/user/insert")
	public String insert() throws Exception{
		 if(userService.isExistUser(user.getName())){
			 roles = roleService.findAll(Role.class);
			 setErrorMessage("error_user_exist");
			 return "add";
		 }
		 user.setPassword(MD5.compute(user.getPassword()));
//		 Set<Role> roles = new HashSet<Role>();
//		 for(Integer id : roleIds){
//			 Role role = new Role();
//			 role.setId(id);
//			 roles.add(role);
//		 }
//		 user.setRoles(roles);
		 userService.save(user);
		 return "r:list";
	}

	@Action("/user/del")
	public String del() throws Exception{
		userService.delete(user);
		return "r:list";
	}
	
	@Action("/user/edit")
	public String edit() throws Exception{
		roles = roleService.findAll(Role.class);
        user = userService.findById(user.getId()); 
		return "edit";
	}
	
	@Action("/user/update")
	public String update() throws Exception{
		User tuser = userService.findById(user.getId());
        if(MD5.compute(oldpassword).equals(tuser.getPassword())){
        	 if(!StringUtils.isBlank(user.getPassword())){
        		tuser.setPassword(MD5.compute(user.getPassword()));
        	 }
	   		 Set<Role> roles = new HashSet<Role>();
			 for(Integer id : roleIds){
				 Role role = new Role();
				 role.setId(id);
				 roles.add(role);
			 }
			 tuser.setRoles(roles);
        	 userService.saveOrUpdate(tuser);
        	return "r:list";
        }else{
    		roles = roleService.findAll(Role.class);
            user = tuser; 
        	setErrorMessage("error_old_password");
        	return "edit";
        }
		
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public List<Integer> getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(List<Integer> roleIds) {
		this.roleIds = roleIds;
	}

	public String getOldpassword() {
		return oldpassword;
	}

	public void setOldpassword(String oldpassword) {
		this.oldpassword = oldpassword;
	}

	public String getCitydatas() {
		return citydatas;
	}

	public void setCitydatas(String citydatas) {
		this.citydatas = citydatas;
	}
	
}
